package com.by.client.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.by.bean.PayResponse;
import com.by.client.PaymentClient;
import com.by.config.AliPayConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class AlipayPayClient implements PaymentClient {

    private AlipayClient alipayClient; // 改为成员变量

    @Autowired
    private AliPayConfig aliPayConfig; // 使用配置类

    @PostConstruct
    public void init() {
        this.alipayClient = new DefaultAlipayClient(
                aliPayConfig.getGatewayUrl(),
                aliPayConfig.getAppId(),
                aliPayConfig.getAppPrivateKey(),
                "json",
                "UTF-8",
                aliPayConfig.getAlipayPublicKey(),
                "RSA2"
        );
    }

    @Override
    public PayResponse unifiedOrder(Double amount, String tradeSn) {
        try {
            // 2. 构建请求参数
            AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
            AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
            model.setOutTradeNo(tradeSn);
            model.setTotalAmount(amount.toString());
            model.setSubject("商品购买");
            model.setProductCode("QUICK_SECURITY_PAY");
            request.setBizModel(model);
            request.setNotifyUrl(aliPayConfig.getNotifyUrl());

            // 3. 调用支付宝接口
            AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
            if (response.isSuccess()) {
                return new PayResponse(response.getTradeNo(), response.getBody(), tradeSn, 1);
            } else {
                throw new RuntimeException("支付宝调用失败：" + response.getMsg());
            }
        } catch (Exception e) {
            throw new RuntimeException("支付宝支付异常", e);
        }

    }

    // 新增网页支付实现
    @Override
    public String createWebPayment(String orderSn, Double amount, String notifyUrl) {
        try {
            AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
            request.setNotifyUrl(notifyUrl);
            request.setBizContent(String.format(
                    "{\"out_trade_no\":\"%s\",\"total_amount\":\"%.2f\",\"subject\":\"商品购买\",\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}",
                    orderSn, amount
            ));
            return alipayClient.pageExecute(request).getBody();
        } catch (AlipayApiException e) {
            throw new RuntimeException("支付宝网页支付异常", e);
        }
    }
}
